/**
 * 3η ΓΕ 2013-2014 - Super Market
 * ΦΡΑΓΚΟΥΛΟΠΟΥΛΟΥ ΕΜΜΑΝΟΥΕΛΑ (69298)
 * ΧΑΛΔΑΣ ΧΑΡΑΛΑΜΠΟΣ (69305)
 * ΧΟΡΜΟΒΙΤΗΣ ΙΩΑΝΝΗΣ (69325)
 * ΧΟΥΣΤΟΥΛΑΚΗ ΕΥΑΓΓΕΛΙΑ (69327)
 */
package authority;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import model.*;

/**
 * Class Εγκρισης Πιστωτικής Κάρτας
 */
public class VerifyCard {

    private String owner;
    private String number;
    private String cvv;
    private String bank;

    public VerifyCard(Customer c) {
        owner = c.getOwner();
        number = c.getCreditCardNum();
        cvv = c.getCvv();
        bank = c.getBank();
    }

    public VerifyCard(String o, String n, String v, String b) {
        owner = o;
        number = n;
        cvv = v;
        bank = b;
    }

    public boolean verify() {
        EntityManager em = EntityManagerFactory.getEntityManager();
        TypedQuery<CreditCardAuthority> q = em.createQuery("SELECT c from CreditCardAuthority c where "
                + " c.cvv='" + cvv + "' AND c.number='"
                + number + "' "
                + " AND c.ownerName='" + owner + "'",
                CreditCardAuthority.class);
        List<CreditCardAuthority> ls = q.getResultList();
        for (int i = 0; i < ls.size(); ++i) {
            if (ls.get(i).getBank().getName().equals(bank)) {
                return true;
            }
        }
        return false;
    }

}
